//
//  HWGameDetailIntroductionCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/15.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameDetailIntroductionCell: UITableViewCell {
    var contentLabel:UILabel!
    var openButton:UIButton!
    var model:HWGameDetailInfoModel? {
        didSet {
            contentLabel.text = model?.content
            print(getTextHeigh(textStr: contentLabel.text!, font: .systemFont(ofSize: 14*widthScale), width: screenWidth-30*widthScale))
            if getTextHeigh(textStr: contentLabel.text!, font: .systemFont(ofSize: 14*widthScale), width: screenWidth-30*widthScale) > 56 {
                openButton.isHidden = false
                if model?.isOpen == true {
                    contentLabel.numberOfLines = 0
                    openButton.setTitle("收起全部", for: .normal)
                } else {
                    contentLabel.numberOfLines = 3
                    openButton.setTitle("展开全部", for: .normal)
                }
                
                openButton.snp.updateConstraints { make in
                    make.top.equalTo(contentLabel.snp.bottom).offset(5*widthScale)
                    make.height.equalTo(20*widthScale)
                }
            }else {
                openButton.isHidden = true
                openButton.setTitle("", for: .normal)
                openButton.snp.updateConstraints { make in
                    make.height.equalTo(0)
                    make.top.equalTo(contentLabel.snp.bottom)
                }
            }
        }
    }
    var openButtonClick:(()->())?
    class func cellWithTableView(tableView:UITableView) -> (HWGameDetailIntroductionCell) {
        let reUse:String = "HWGameDetailIntroductionCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameDetailIntroductionCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameDetailIntroductionCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        contentLabel = HWLabel.createLabel(title: "", color: "#888888", font: 14*widthScale)
        contentLabel.numberOfLines = 3
        contentView.addSubview(contentLabel)
        
        openButton = UIButton(type: .custom)
        openButton.setTitle("展开全部", for: .normal)
        openButton.setTitleColor(.colorWithHexString(hexString: "#F77C2F"), for: .normal)
        openButton.titleLabel?.font = .boldSystemFont(ofSize: 14*widthScale)
        openButton.backgroundColor = .clear
        openButton.addTarget(self, action: #selector(openButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(openButton)
    }
    
    func initFrame() -> Void {
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.top.equalTo(10*widthScale)
            make.right.equalTo(-15*widthScale)
        }
        
        openButton.snp.makeConstraints { make in
            make.right.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(5*widthScale)
            make.height.equalTo(20*widthScale)
            make.bottom.equalTo(-30*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}

extension HWGameDetailIntroductionCell {
    @objc func openButtonClick(_:UIButton) -> Void {
        self.model?.isOpen = !(self.model?.isOpen ?? false)
        self.openButtonClick?()
    }
}
